package cn.gz.demo.ui;


import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;
import cn.gz.demo.adapter.RecyclerAdapter;
import cn.gz.demo.bean.ModelBean;
import cn.gz.demo.databinding.RecylerViewBinding;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * Created by Liuyz on 2016/12/6.
 */
public class RecyclerViewActivity extends BaseActivity implements AdapterView.OnItemSelectedListener, View.OnClickListener {
    private RecylerViewBinding binding;
    private List<ModelBean> beanList;
    private RecyclerAdapter adapter;
    private String des[] = {"云层里的阳光", "好美的海滩", "好美的海滩", "夕阳西下的美景", "夕阳西下的美景"
            , "夕阳西下的美景", "夕阳西下的美景", "夕阳西下的美景", "好美的海滩"};

    private int resId[] = {R.mipmap.img1, R.mipmap.img2, R.mipmap.img2, R.mipmap.img3,
            R.mipmap.img4, R.mipmap.img5, R.mipmap.img3, R.mipmap.img1};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);

        initViews();
        initData();
        initSpinner();
    }

    private void initViews() {

        if (null != binding.toolbar) {
//            setSupportActionBar(toolbar);
            if (null != binding.toolbarTitle) {
                binding.toolbarTitle.setText(getTitle());
            }
        }

        binding.buttonAdd.setOnClickListener(this);

        //设置布局显示方式
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, true));
        //设置添加删除item时候的动画
        binding.recyclerview.setItemAnimator(new DefaultItemAnimator());
    }

    private void initData() {
        beanList = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            ModelBean bean = new ModelBean();
            bean.setResId(resId[i]);
            bean.setTitle(des[i]);
            beanList.add(bean);
        }
        adapter = new RecyclerAdapter(this, beanList);
        binding.recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object object) {
                Toast.makeText(RecyclerViewActivity.this, ((ModelBean) object).getTitle(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initSpinner() {
        String categorys[] = this.getResources().getStringArray(R.array.categorys);
        ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categorys);

        // 为adapter设置下拉菜单样式
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // spinner设置adapter
        binding.toolbarCategory.setAdapter(adapter);
        binding.toolbarCategory.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                binding.recyclerview.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, true));
                break;
            case 1:
                binding.recyclerview.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, true));
                break;
            case 2:
                binding.recyclerview.setLayoutManager(new GridLayoutManager(this, 2));
                break;
            case 3:
                binding.recyclerview.setLayoutManager(new GridLayoutManager(this, 2, RecyclerView.HORIZONTAL, true));
                break;
            case 4:
                binding.recyclerview.setLayoutManager(new StaggeredGridLayoutManager(2, RecyclerView.VERTICAL));
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onClick(View v) {
        ModelBean bean = new ModelBean();
        bean.setTitle("这是新添加的");
        bean.setResId(R.mipmap.img5);
        beanList.add(0, bean);
        adapter.notifyDataSetChanged();//更新全部数据
        adapter.notifyItemInserted(0);//在
        adapter.notifyItemRemoved(0);
        adapter.notifyItemChanged(0);
        adapter.notifyItemMoved(0, 1);
        adapter.notifyItemRangeChanged(0, 2);
        adapter.notifyItemRangeInserted(0, 2);
        adapter.notifyItemRangeRemoved(0, 2);
    }
}
